package com.adxon.filter;

import com.adxon.MsgException;
import com.adxon.Uri;
import com.adxon.data.Item;
import com.adxon.data.Picture;
import com.adxon.servlet.ServletBase;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PictFilter extends FilterBase{
	public void doGet(HttpServletRequest req,HttpServletResponse resp, 
			FilterChain chain)throws IOException,ServletException,
			InstantiationException,IllegalAccessException,
			MsgException{
		System.out.println("PictFilter: "+new Uri(req).toString());
		Picture o=(Picture)((Item)new Item().update(new Uri(req).toString())).
				extend();
		if(!(o instanceof Picture))
			throw new MsgException(MsgException.NOT_FOUND,null);
		ServletBase.cacheControl(resp,360000);	// 100 hours
		resp.setContentType(o.getContentType());
		resp.getOutputStream().write(o.getBits());
	}
}